#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os,sys
#import printc.printc as printc

#path = "rename/对应关系文件.txt"

#dirpath = "待改名文件夹"


def rename(path,dirpath):
    fp = open(path, "r")
    sample = fp.readlines()
    result_dict = {}

    for line in sample:
        sample_ = line.split('\t') #按tab键分割
        result_dict[sample_[0]]=sample_[1].strip('\n')

    path=os.path.realpath(path)
    filename_list = os.listdir(dirpath)
    with open(path, 'r') as file:
        lines = file.readlines()
    m = []
    for x, f in enumerate(filename_list):
        Olddir = os.path.join(dirpath, f)  
        if os.path.isdir(Olddir):  
            continue
        filename = os.path.splitext(f)[0]  
        filetype = os.path.splitext(f)[1]  
        
        try:
            new_name=  result_dict[filename]+ filetype
            

            old_path_name =  './' + dirpath + '/' + f
            new_path_name = './' + dirpath + '/'+ new_name
            if os.path.exists(new_path_name):
                #print('已存在,即将删除：'+new_path_name)
                os.remove(new_path_name)
            os.rename(old_path_name,new_path_name)
        except Exception:
            print('错误：'+filename+'，对应关系不存在') 
            #printc.printf('错误：'+filename+'，对应关系不存在','red')  #

    print('已对文件['+dirpath+']内文件全部重命名') 
    #printc.printf('已对文件['+dirpath+']内文件全部重命名','green') #

def main():
    args1= sys.argv[1]
    args2= sys.argv[2]

    # printc.printf('【批量重命名工具】','yellow') 
    # printc.printf('根据['+args1+']对应关系,批量重命名['+args2+']内的全部文件','green') #
    # printc.printf('警告：重命名操作不可逆，如有需要请提前备份数据','yellow') 

    print('【批量重命名工具】') 
    print('根据['+args1+']对应关系,批量重命名['+args2+']内的全部文件') 
    print('警告：重命名操作不可逆，如有需要请提前备份数据') 
    #os.system( 'pause' )
    rename(args1,args2)

if __name__ == "__main__":
    main()